import Vue from 'vue'
import Vuex from 'vuex'
import * as types from './mutation-types'
Vue.use(Vuex)

const state = {
  indexData: {
    catelist: [],
    indexlist: [],
    productlist: [],
    indexadv: [],
    topadv: [],
    userinfo: {},
    titleadv: [],
    textadv: [],
    packageadv: [],
    personadv: []
  },
  community: {
    cname: '',
    cid: ''
  },
  communitys: [],
  keyword: '',
  productdetail: {},
  usercar: 0,
  addpro: null,
  coupon: false,
  server: {},
  integral: {},
  integralHistory: {},
  allcount: {},
  lifeserver: {},
  active: {},
  estateList: {},
  house: {},
  room: {},
  roomList: {},
  communityId: {
    com: {
      name: '-请选择小区-',
      id: ''
    },
    house: '',
    room: '',
    dong: ''
  },
  delever: {
    receiver: {},
    receiverDetail: {},
    sender: {},
    senderDetail: {}
  },
  timelist: {}
}

const mutations = {
  [types.GETCATELIST] (state, catelist) {
    state.indexData.catelist = catelist
  },
  [types.GETINDEXLIST] (state, indexlist) {
    state.indexData.indexlist = indexlist
  },
  [types.GETPRODUCTLIST] (state, productlist, callback, isIndex = false) {
    if (productlist.length !== 0 && productlist[0].catid === '2633' && isIndex) {
      state.server = productlist
    } else {
      state.indexData.productlist = productlist
    }
    callback && callback(state.indexData.productlist.length === 0)
  },
  [types.GETINDEXADV] (state, indexadv, isbanner) {
    if (isbanner === '68') {
      state.indexData.indexadv = indexadv
    } else if (isbanner === '67') {
      state.indexData.topadv = indexadv
    } else if (isbanner === '73') {
      state.indexData.textadv = indexadv
    } else if (isbanner === '71') {
      state.indexData.titleadv = indexadv
    } else if (isbanner === '72') {
      state.indexData.packageadv = indexadv
    } else if (isbanner === '74') {
      state.indexData.personadv = indexadv
    }
  },
  [types.GETUSERINFO] (state, info, callback = () => {}) {
    state.indexData.userinfo = info
  },
  [types.CHANGECOMMUNITY] (state, community) {
    state.community = community
  },
  [types.GETCOMMUNITYS] (state, communitys) {
    state.communitys = communitys
    state.communitys ? state.community = communitys[0] : ''
  },
  [types.GETPRODUCTDETAIL] (state, productdetail, callback = function () {}) {
    state.productdetail = productdetail
    callback()
  },
  [types.CLEARPRODUCTDETAIL] (state) {
    state.productdetail = {}
  },
  [types.GETUSERCAR] (state, usercar) {
    usercar = usercar !== '' ? usercar : 0
    state.usercar = usercar
  },
  [types.BUYNOW] (state, addpro) {
    if (addpro) {
      state.addpro = addpro
    }
  },
  [types.ADDTOCAR] (state, addcar, callback = function () {}) {
    callback(addcar.id)
  },
  [types.COUPON] (state, coupon) {
    state.coupon = coupon
  },
  [types.INTEGRAL] (state, integral, callback = () => {}) {
    state.integral = integral
    callback()
  },
  [types.INTEGRAL_HISTORY] (state, integralHistory, callback = () => {}) {
    state.integralHistory = integralHistory
    callback()
  },
  [types.ALLCOUNT] (state, allcount, callback = () => {}) {
    state.allcount = allcount
    callback()
  },
  [types.LIFESERVER] (state, life, callback = () => {}) {
    state.lifeserver = life
    callback()
  },
  [types.ACTIVITY] (state, active, callback = () => {}) {
    state.active = active
    callback(state.active)
  },
  [types.GETESTATELIST] (state, estateList) {
    state.estateList = estateList
  },
  [types.GETHOUSE] (state, house, callback = () => {}) {
    state.house = house
    callback()
  },
  [types.GETROOM] (state, room, type, callback = () => {}) {
    if (type !== '4') {
      state.room = room
    } else {
      state.roomList = room
    }
    callback()
  },
  CLEARROOM (state) {
    state.room = {}
    state.roomList = {}
  },
  COMMUNITYID (state, data) {
    state.communityId = Object.assign(state.communityId, data)
  },
  [types.DELEVERRECEIVE] (state, data) {
    state.delever.receiver = data
  },
  [types.RECEIVEDETAIL] (state, data) {
    state.delever.receiverDetail = data
  },
  [types.TIMELIST] (state, data) {
    state.timelist = data
  }
}

export default new Vuex.Store({
  state,
  mutations
})
